import {getAddressData,getSubCatelog,getMianData,login,getIndustryMerchantV24,getSubMerchant} from '../../../Tools/HTTPOpertion.js';
import {getUserLocation} from '../../../Tools/index.js'
Page({
    data: {
        city:'北京',
        page: 1,
        obj: {},
        windowHeight: 1200,
        Popup_inde: 0,
        Popup_inde2: 0,
        PopupIf: false,
        unionidPopupIf: false,
        name: '',
        list: [],
        className: "",
        scrollHeight: 0,
        positionType: "nor",
        top: 0,
        paddingTop: 0,
        subList: [],
        typeList: [],
        Popup_index_right: 0,
        types: "全部分类",
        orderType: "智能排序",
        superID: 0,
        classID: 0,
        orderID: "",
        flag:true,
        ref:false,
        startJZ:false
    },
    onLoad: function (options) {
        var self = this;
        wx.getSystemInfo({
            success: function (e) {
                self.setData({
                    windowHeight: e.windowHeight,
                    scrollHeight: e.windowHeight
                })
            }
        });
        getUserLocation('homePage',function (addr) {
            getAddressData({ location: addr }).then(function (result) {
                self.setData({'city':result.data?result.data.id:''})
                wx.setStorageSync("level", result.data?result.data.level:'');
                self.loadData();
                self.windowHeight();
            })
            .catch(function (err) {
                wx.showToast({
                    title: err.message,
                    icon: 'none',
                    duration: 2000
                })
            });
        })
    },
    PopupF: function () {
        this.setData({
            PopupIf: false,
            unionidPopupIf: false
        })
    },
    PopupBoxLeftChoose: function (e) {
        var self = this;
        var id = typeof e == "number" ? 10 : e.currentTarget.dataset.id;
        console.log(e);

        if (id == 0) {
            self.setData({
                subList: [],
                Popup_inde: 0,
                types: "全部",
                superID: e.currentTarget ? e.currentTarget.dataset.id : 0
            });
            this.loadData();
            this.returnType();
            return;
        }
        self.setData({
            superID: e.currentTarget.dataset.id,
            Popup_index_right: 0
        });
        getSubCatelog({ intPara2: wx.getStorageSync("city"), level: wx.getStorageSync("level"), latitude: wx.getStorageSync("latitude"), longitude: wx.getStorageSync("longitude"), intPara: self.data.superID }).then(function (result) {
            var temp = [{ name: "不限", id: 0 }].concat(result.data.list);
            console.log(result);
            self.setData({
                subList: temp,
                Popup_inde: e.currentTarget.dataset.index,
                types: e.currentTarget.dataset.name
            });
        })
        .catch(function (error) {

        });
    },
    navListTap: function (e) {
        var name = e.currentTarget.dataset.name;
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/others/shopList/shopList?name=' + name + "&id=" + id + "&index=" + e.currentTarget.dataset.index
        })
        this.returnType();
    },
    scrolling: function (e) {
        if (this.data.positionType === "nor") {
            if (e.detail.scrollTop > (47 + 52)) {
                this.setData({
                    positionType: "fixed",
                    PopupIf: false,
                    unionidPopupIf: false,
                    paddingTop: 92 + 15
                });
            }
        } else {
            if (e.detail.scrollTop < (47 + 52)) {
                this.setData({
                    positionType: "nor",
                    PopupIf: false,
                    unionidPopupIf: false,
                    paddingTop: 0
                });
            }
        }


    },
    yhTap: function (e) {
        var name = e.currentTarget.dataset.name;
        wx.navigateTo({
            url: '/pages/others/purchaseCoupon/purchaseCoupon'
        })
        this.returnType();
    },
    tsTap: function (e) {
        var name = e.currentTarget.dataset.name;
        wx.navigateTo({
            url: '/pages/others/exploreExperience/exploreExperience'
        })
        this.returnType();
    },
    shopTap: function (e) {
        var name = e.currentTarget.dataset.name;
        console.log(e.currentTarget.dataset.index);
        wx.navigateTo({
            url: '/pages/others/MerchantDetails/MerchantDetails?id=' + e.currentTarget.dataset.id + "&index=" + e.currentTarget.dataset.index + "&qFlag=" + e.currentTarget.dataset.qFlag
        })
        this.returnType();
    },
    //
    inputTap: function (e) {
        wx.navigateTo({
            url: '/pages/others/search/search'
        })
        this.returnType();
    },
    //
    dkTap: function (e) {
        var pic_url = e.currentTarget.dataset.pic_url;
        wx.navigateTo({
            url: '/pages/others/webApp/webApp?url=' + pic_url
        })

        this.returnType();
    },
    //获取首页信息
    loadData: function () {

        wx.showLoading({
            title: '加载中'
        })

        var self = this;
        var config = { intPara2: wx.getStorageSync("level"), cnd: self.data.city, latitude: wx.getStorageSync("latitude"), longitude: wx.getStorageSync("longitude") };
        console.log(config);
        getMianData(config).then(function (result) {
            console.log(result);
            var temp = [{ name: "全部", id: 0 }].concat(result.data.catalogList);

            self.setData({
                subList: [{ name: "不限", id: null }],
                obj: result.data,
                typeList: temp
            });
            self.loadList(1);
            wx.stopPullDownRefresh();
        })
        .catch(function (error) {
            wx.stopPullDownRefresh()
        });
    },
    windowHeight: function () {
        var windowHeight = wx.getStorageSync('windowHeight');
        this.setData({
            windowHeight: windowHeight
        })
    },

    returnType: function () {
        this.setData({
            unionidPopupIf: false,
            PopupIf: false
        })
    },
    shTap: function () {
        this.setData({
            unionidPopupIf: false,
            PopupIf: !this.data.PopupIf
        })
        if (this.data.positionType === "fixed") {
            this.setData({
                top: "top"
            })
        }
    },

    unionidTap: function () {
        this.setData({
            PopupIf: false,
            unionidPopupIf: !this.data.unionidPopupIf
        })
    },
    //
    PopupBoxLeft: function (e) {
        wx.showLoading({
            title: '加载中',
        })
        var self = this;
        var id = e.currentTarget.dataset.id;

        console.log(e);
        if (id == 0) {
            self.setData({
                Popup_index_right: 0,
                orderType: "全部",
                classID: 0
            });
        } else {
            this.setData({
                Popup_index_right: e.currentTarget.dataset.index,
                types: e.currentTarget.dataset.name,
                classID: id
            })
        }

        var config = e.currentTarget.dataset.id == 0 ? { intPara2: wx.getStorageSync("level"), cnd: wx.getStorageSync("city"), latitude: wx.getStorageSync("latitude"), longitude: wx.getStorageSync("longitude"), intPara: self.data.superID } : { latitude: wx.getStorageSync("latitude"), longitude: wx.getStorageSync("longitude"), intPara: e.currentTarget.dataset.id, intPara2: wx.getStorageSync("level"), cnd: wx.getStorageSync("city"), unionid: self.data.orderID };
        getIndustryMerchantV24(config).then(function (result) {
            self.setData({
                list: result.data.list || [],
                PopupIf: !self.data.PopupIf,
                unionidPopupIf: false,
                startJZ:true
            });
        })
        .catch(function (error) {
        });
    },
    //
    PopupBoxRight: function (e) {
        wx.showLoading({
            title: '加载中',
        })
        this.setData({
            Popup_inde2: e.currentTarget.dataset.index,
            orderType: e.currentTarget.dataset.name,
            orderID: e.currentTarget.dataset.id
        })

        var self = this;
        getIndustryMerchantV24({ latitude: wx.getStorageSync("latitude"), longitude: wx.getStorageSync("longitude"), unionid: e.currentTarget.dataset.id, intPara2: wx.getStorageSync("level"), cnd: wx.getStorageSync("city"), intPara: self.data.classID }).then(function (result) {
            self.setData({
                list: result.data.list || [],
                PopupIf: false,
                unionidPopupIf: !self.data.unionidPopupIf,
                startJZ:true
            });
        })
        .catch(function (error) {
        });
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({
            page: ++this.data.page
        })
        this.loadList(this.data.page);
    },
    loadList: function (page) {
        var self = this;
        getSubMerchant({ latitude: wx.getStorageSync("latitude"), longitude: wx.getStorageSync("longitude"), page: self.data.page }).then(function (result) {
            wx.hideLoading();
            self.setData({
                list: self.data.list && self.data.list.concat(result.data.list),
                flag:true,
                ref: false,
                startJZ:true
            });
            wx.stopPullDownRefresh();
            if (result.data.list.length == 0) {
                wx.showToast({
                    title: '没有更多数据',
                    icon: 'success',
                    duration: 2000
                })
            }
        })
        .catch(function (error) {
            wx.hideLoading();
            wx.stopPullDownRefresh()
        });
    },
    scrolltoupper: function () {
      if (this.data.flag) {
          this.setData({
            list:[],
            page:1,
            flag:false,
            ref: true
          })
          var that = this;
          setTimeout(function () {
            that.loadList(1);
          }, 1000)
      }
    }
})
